package com.laikan.reader.bookfriend.holder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;

/**
 * Created by admin on 2016/10/31.
 * 通用的recyclerView的ViewHolder
 */
public class LViewHolder extends RecyclerView.ViewHolder{

    private View mConvertView;
    private SparseArray<View> mViews;
    private Context mContext;
    public LViewHolder(View itemView){
        super(itemView);
    }

    public LViewHolder(Context context, View itemView) {
        super(itemView);
        mContext = context;
        mConvertView = itemView;
        mViews = new SparseArray<View>();
    }

    public static LViewHolder createViewHolder(Context context, int layoutId){
        View itemView = LayoutInflater.from(context).inflate(layoutId, null);
        LViewHolder holder = new LViewHolder(context,itemView);
        return  holder;
    }

    public <T extends View> T getView(int viewId){
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId,view);
        }
        return (T)view;
    }

    public View getRootView(){
        return mConvertView;
    }
}
